﻿#include "Plane.h"
#include"qdebug.h"
#include"WeaponBank.h"
Plane::Plane(const int x, const int y)
{
	move(x, y);
	init();
	//creatorWeaponBank();
}

Plane::Plane(const QPoint& pos)
{
	move(pos);
	init();
	//creatorWeaponBank();
}

Plane::~Plane()
{
	//玩家子弹库
	if (m_WeaponBank != nullptr)
	{
		auto temp = m_WeaponBank;
		m_WeaponBank = nullptr;
		delete m_WeaponBank;
	}
}

void Plane::init()
{
	setType(Type::plane);
	m_blood.addColor(0.7, Qt::GlobalColor::green, Qt::GlobalColor::green);
	m_blood.addColor(0.3, Qt::GlobalColor::yellow, Qt::GlobalColor::yellow);
	m_blood.addColor(0.0, Qt::GlobalColor::red, Qt::GlobalColor::red);
	
}

void Plane::update()
{
	if (!isActive()||m_pause)
		return;
	if (m_Hp <= 0)
	{
		getModel()["死亡"].update();
		if (getModel()["死亡"].finished())
		{
			//qDebug() << "敌机已完全死亡";
			destroy();
		}
			
	}
	else
	{
		getModel()["正常"].update();
		//更新血条坐标
		m_blood.move(getPos() + m_bloodPoint);
		
	}
	//qDebug() << m_WeaponBank;
	
}

void Plane::draw(QPainter* painter)
{
	//绘制飞机
	if (!isActive())
		return;
	if (m_Hp <= 0)
	{
		getModel()["死亡"].draw(painter, getModelRect().topLeft());
	}
	else
	{
		getModel()["正常"].draw(painter, getModelRect().topLeft());
	}
	//绘制血条
	drawBlood(painter);
	
}
void Plane::drawBlood(QPainter* painter)
{
	if (!m_flagBlood||getHp()<=0)
		return;
	qreal num = qreal(getHp()) / qreal(getMaxHP());
	//qDebug() << num;
	m_blood.draw(painter, num);
}
void Plane::setProperty(const QVector2D& vector, const int Steplength)
{
	m_vector = vector;
	m_Steplength = Steplength;
	//setTimer(delay);
}
void Plane::startBlood(const QPoint& bloodPoint,const QSize& bloodSize)
{
	m_bloodPoint = bloodPoint;
	m_blood.setSize(bloodSize);
	m_flagBlood = true;
}
void Plane::startBlood(int space, const Direction& dir)
{
	//QPoint pos = getPos();
	QSize size = getSize();
	int h = 5;//血条高度
	if (dir == Direction::up)
	{
		m_bloodPoint = QPoint(0,-space);
	}
	else if (dir == Direction::down)
	{
		m_bloodPoint = QPoint(0, size.height()+space);
	}
	m_blood.setSize(QSize(size.width(), h));
	m_flagBlood = true;
}
void Plane::closeBlood()
{
	m_flagBlood = false;
}

void Plane::setPause(bool flag)
{
	Articles::setPause(flag);
}

WeaponBank* Plane::getWeaponBank()
{
	return m_WeaponBank;
}

void Plane::creatorWeaponBank()
{
	//创建玩家武器库
	if (m_WeaponBank == nullptr)
	{
		
		//m_playBullet->setCollider(QRect(QPoint(0, 0), m_Size));
		//qDebug() << getWindowSize();
		if (getWindowSize().isEmpty())
		{
			OutInfo;
			qDebug() << "请先设置setWindowSize()";
			return;
		}
		m_WeaponBank = new WeaponBank(50,m_parent);
		//if (type() == Entity::Type::enemyPlane)
		/*{
			qDebug() << getWindowSize()<< m_WeaponBank;
		}*/
		m_WeaponBank->setWindowSize(getWindowSize());
		m_WeaponBank->setProperty(QVector2D(0, -1), 500);
		m_WeaponBank->setBulletTimer(200);
	}
}

void Plane::creatorWeaponBank(WeaponBank* weaponBank)
{
	m_WeaponBank = weaponBank;
}
